# coding:utf-8
# writingtime: 2022-8-4
# reference: https://doi.org/10.1007/s40815-021-01243-2

from DistanceFunction.euclidean import Euclidean


class WL:
    def __init__(self, dataList, membershipMatrix, clusterCenter):
        """
        function
        :param dataList: 样本向量
        :param membershipMatrix: 关系矩阵
        :param clusterCenter: 聚合中心
        """
        self.dataList = dataList
        self.membershipMatrix = membershipMatrix
        self.clusterCenter = clusterCenter

    def getsum1(self):
        """
        function: 计算上一部分的结果
        :return:
        """
        sum1 = 0
        for i in range(len(self.clusterCenter)):
            sum2 = 0
            sum3 = 0
            for j in range(len(self.dataList)):
                sum2 += (self.membershipMatrix[i][j] ** 2) * (
                    Euclidean.getresult(self.dataList[j], self.clusterCenter[i]))
                sum3 += self.membershipMatrix[i][j]
            sum1 += (sum2 / sum3)

    def getminmed(self):
        """
        function: 计算最小值和中间值
        :return: 最小，中间
        """
        # 计算最小
        li_temp1 = []
        # 计算中间值
        li_temp2 = []
        for i in range(len(self.clusterCenter)):
            for k in range(len(self.clusterCenter)):
                if i != k:
                    li_temp1.append(Euclidean.getresult(self.clusterCenter[i], self.clusterCenter[k]) ** 2)
                li_temp2.append(Euclidean.getresult(self.clusterCenter[i], self.clusterCenter[k]) ** 2)
        minvalue=min(li_temp1)

